package de.gameduell.framework.util { 
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.utils.getTimer;		

	/**
	 * @author Gevorg Sargsyan
	 * 
	 */
	public class SyncTimer extends EventDispatcher {

		protected var _timeline:DisplayObject;
		protected var _lastTime:Number;
		protected var _passedTime:uint;

		public function SyncTimer( timeline:DisplayObject ) {
			_timeline = timeline;
		}

		/**
		 * @return the passedTime in milliseconds between last and previous frames  
		 */
		public function get passedTime():int {
			return _passedTime;
		}

		/**
		 * Starts listening timeline ENTER_FRAME 
		 */
		public function start():void {
			_lastTime = getTimer();
			_timeline.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
		}

		/**
		 * Stops listening timeline ENTER_FRAME 
		 */
		public function stop():void {
			_timeline.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
		}

		protected function enterFrameHandler(event:Event):void {
			var currTime:uint = getTimer();
			_passedTime = currTime - _lastTime;
			_lastTime = currTime;
			dispatchEvent(new TimerEvent(TimerEvent.TIMER));
		}
	}
}